package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Loader;
import com.tianxin.workbao.workBao.bean.LoaderAndLoaderUse;
import com.tianxin.workbao.workBao.bean.LoaderUse;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.MyPopupWindowListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by hacker on 2017/7/2.
 */

public class SelectLoaderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<LoaderAndLoaderUse> loaderList;
    private Context mContent;
    private MyPopupWindowListener myPopupWindowListener;

    public SelectLoaderAdapter(List<LoaderAndLoaderUse> loaderList, Context content, MyPopupWindowListener listener) {
        this.loaderList = loaderList;
        this.mContent = content;
        this.myPopupWindowListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        if (loaderList.size() > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == 1) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.select_loader_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            LoaderAndLoaderUse loaderAndLoaderUse = loaderList.get(position);
            Loader loader = loaderAndLoaderUse.getLoader();
            LoaderUse loaderUse = loaderAndLoaderUse.getLoaderUse();
            viewHolder.loaderNumber.setText(loader.getName());
            viewHolder.sortTv.setText((position + 1) + "");
            if (loaderUse == null) {
                viewHolder.use_Lv.setVisibility(View.GONE);
                viewHolder.useStatus.setText(R.string.loader_free);
            } else {
                viewHolder.use_Lv.setVisibility(View.VISIBLE);
                if (loaderUse.getUserId() == SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0)) {
                    viewHolder.userName.setText("自己占用中");
                } else {
                    viewHolder.userName.setText(loaderUse.getUserName());
                }
                viewHolder.useStatus.setText(R.string.loader_usesing);
            }
            viewHolder.setViewListener(loaderAndLoaderUse);
        }
    }

    @Override
    public int getItemCount() {
        if (loaderList.size() > 0) {
            return loaderList.size();
        } else {
            return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        RelativeLayout mItemView;
        TextView sortTv;
        TextView loaderNumber;
        LinearLayout use_Lv;
        CircleImageView useUserHeader;
        TextView userName;
        TextView useStatus;

        public ViewHolder(View itemView) {
            super(itemView);
            mItemView = (RelativeLayout) itemView.findViewById(R.id.select_item_view);
            sortTv = (TextView) itemView.findViewById(R.id.loader_sort);
            loaderNumber = (TextView) itemView.findViewById(R.id.select_loader_number);
            use_Lv = (LinearLayout) itemView.findViewById(R.id.use_people_view);
            useUserHeader = (CircleImageView) itemView.findViewById(R.id.use_user_header);
            userName = (TextView) itemView.findViewById(R.id.use_user_name);
            useStatus = (TextView) itemView.findViewById(R.id.use_status);
        }

        public void setViewListener(final LoaderAndLoaderUse loaderAndLoaderUse) {
            mItemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (loaderAndLoaderUse.getLoaderUse() != null) {
                        myPopupWindowListener.OnMyClick(-1, 0, null);
                    } else {
                        myPopupWindowListener.OnMyClick(Constant.SELECT_TYPE_LOADER,
                                Integer.valueOf(loaderAndLoaderUse.getLoader().getId().toString()),
                                loaderAndLoaderUse.getLoader().getName());
                    }
                }
            });
        }
    }
}
